package com.luoke.context;


import com.luoke.pojo.dto.BagDTO;
import com.luoke.pojo.entity.BattlePet;
import com.luoke.pojo.entity.Pet;
import com.luoke.pojo.entity.User;

import java.util.List;

/**
 * 该类用来保存当前用户的相关信息 以供全局使用
 */
public class UserContext {

    /**
     * ThreadLocal 用于存储每个线程独立的当前用户
     */
    private static final ThreadLocal<String> currentUser = new ThreadLocal<>();
    private static final ThreadLocal<BattlePet> currentPet = new ThreadLocal<>();
    private static final ThreadLocal<User> User = new ThreadLocal<>();
    private static final ThreadLocal<List<Pet>> UserPet = new ThreadLocal<>();
    private static final ThreadLocal<List<BagDTO>> UserBag = new ThreadLocal<>();

    public static void setUserBag(List<BagDTO> bag) {
        UserBag.set(bag);
    }

    public static List<BagDTO> getUserBag() {
        return UserBag.get();
    }

    public static void setUserPet(List<Pet> pet) {
        UserPet.set(pet);
    }

    public static List<Pet> getUserPet() {
        return UserPet.get();
    }

    /**
     * 设置当前连接的用户
     */
    public static void setCurrentUser(String username) {
        currentUser.set(username);
    }

    public static void setUser(User user) {
        User.set(user);
    }

    public static User getUser() {
        return User.get();
    }

    /**
     * 获取当前连接的用户
     */
    public static String getCurrentUser() {
        return currentUser.get();
    }

    public static void setCurrentPet(BattlePet pet) {
        currentPet.set(pet);
    }

    /**
     * 获取用户出战的宠物信息
     */
    public static BattlePet getCurrentPet() {
        return currentPet.get();
    }

    /**
     * 清除当前线程的用户信息
     */
    public static void clear() {
        currentUser.remove();

    }
}
